﻿using Lhz.InternetService;
using Lhz.Sc.Entity.Exception;
using Lhz.Sc.Entity.Interface;
using Lhz.Serialization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Web.Script.Services;
using System.Web.Services;

namespace Lhz.Sc.Ms.Html.service
{
    /// <summary>
    /// WebServiceApi 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class WebServiceApi : WebService
    {
        private JsonSerializer js = new JsonSerializer();
        private const string ServiceInvokerSessionKey = "ServiceInvoker";

        private ServiceInvoker ServiceInvoker
        {
            get
            {
                var result = Application[ServiceInvokerSessionKey] as ServiceInvoker;
                if (result == null)
                {
                    result = new ServiceInvoker(typeof(InvokeArgument), typeof(InvokeResult)) { AllowCookie = true };
                    Application[ServiceInvokerSessionKey] = result;
                }
                return result;
            }
        }

        [WebMethod]
        public string InvokeByInvokeArgument(InvokeArgument invokeArgument)
        {
            var url = ConfigurationManager.AppSettings["ServiceCenterUrl"];
            var username = ConfigurationManager.AppSettings["ServiceCenterUsername"];
            var password = ConfigurationManager.AppSettings["ServiceCenterPassword"];
            InvokeResult result;
            try
            {
                var pathCurrent = Environment.CurrentDirectory;
                result = ServiceInvoker.Invoke<InvokeResult>(url, "InvokeByJsonInvokeArgument", invokeArgument);
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException.Message == new SessionNotFoundException().Message)
                {
                    ServiceInvoker.Invoke(url, "Login", username, password);
                    result = ServiceInvoker.Invoke<InvokeResult>(url, "InvokeByJsonInvokeArgument", invokeArgument);
                }
                else
                {
                    throw e;
                }
            }
            return js.Serialize(result);
        }

        [WebMethod]
        public string InvokeByInvokeArguments(InvokeArgument[] invokeArguments)
        {
            var url = ConfigurationManager.AppSettings["ServiceCenterUrl"];
            var username = ConfigurationManager.AppSettings["ServiceCenterUsername"];
            var password = ConfigurationManager.AppSettings["ServiceCenterPassword"];
            InvokeResult[] result;
            try
            {
                result = ServiceInvoker.Invoke<InvokeResult[]>(url, "InvokeByJsonInvokeArguments", invokeArguments);
            }
            catch
            {
                ServiceInvoker.Invoke(url, "Login", username, password);
                result = ServiceInvoker.Invoke<InvokeResult[]>(url, "InvokeByJsonInvokeArguments", invokeArguments);
            }
            return js.Serialize(result);
        }

        [WebMethod]
        public string GetGuid()
        {
            return Guid.NewGuid().ToString();
        }

        [WebMethod]
        public List<string> GetGuids(int count)
        {
            var result = new List<string>();
            for (int i = 0; i < count; i++)
            {
                result.Add(Guid.NewGuid().ToString());
            }
            return result;
        }
    }
}
